#include "eveCommon.h"
#include "eveGuiButton.h"
#include "eveTheme.h"

CGuiButton::CGuiButton(CGuiControl* parent,const String& type) 
: CGuiControl(parent,type)
{

}

CGuiButton::~CGuiButton()
{
}

void CGuiButton::Create(CGuiElementHolder* holder,uint32 id,uint32 style)
{
	CGuiControl::Create(holder,id,style);
	if( !m_pCtrlRender ) m_pCtrlRender = new CGuiButtonRender(*this,holder);
}
void CGuiButton::ParseExtendProperty(TiXmlElement* element)
{

	if( !element ) return;
	XMLElementWrapper wrapper(element);

	const String& name = wrapper.GetStringValue("name");
	if( name == "font" )
	{
		this->SetFont(StringUtil::ToInt(wrapper.GetNodeText()));
	}
	else if( name == "color" )
	{
		this->SetTextColor(StringUtil::ToColor(wrapper.GetNodeText()));
	}
	else if( name == "text" )
	{
		this->SetText( wrapper.GetNodeText() );
	}
	
	

	
}



bool CGuiButton::HandleEvent(uint32 id,GUI_MESSAGE msg,CGuiControl* p )
{
	if( p == this )
	{
		Inputer* inputsys = Inputer::GetSingleton();
		const stPointI& pt = inputsys->GetMousePos();
		if( m_style & BUTTON_PRESSED )
		{
			switch( msg )
			{
			case GUI_CONTROL_MOUSE_PRESSED:
				this->SetState(GUI_CONTROL_STATE_PRESSED);
				return true;
			case GUI_CONTROL_MOUSE_RELEASE:
				if( this->ContainPoint(pt) )
				{
					this->SetState(GUI_CONTROL_STATE_MOUSEOVER);
					this->SendEvent(m_id,GUI_BUTTON_CLICKED,p);
				}
				else
					this->SetState(GUI_CONTROL_STATE_NORMAL);
				return true;
			case GUI_CONTROL_MOUSE_ENTER:
				this->SetState(GUI_CONTROL_STATE_MOUSEOVER);
				return true;
			case GUI_CONTROL_MOUSE_LEAVE:
				this->SetState(GUI_CONTROL_STATE_NORMAL);
				return true;
			}
		}
		else if( m_style & BUTTON_CHECKED )
		{
			switch( msg )
			{
			case GUI_CONTROL_MOUSE_PRESSED:
				{
					GUI_CONTROL_STATE state = this->GetState();
					if( state == GUI_CONTROL_STATE_CHECKED )
					{
						this->SetState(GUI_CONTROL_STATE_MOUSEOVER);
						this->SendEvent(m_id,GUI_BUTTON_UNCHECKED,p);
					}
					else //if( state = GUI_CONTROL_STATE_MOUSEOVER )
					{
						this->SetState(GUI_CONTROL_STATE_CHECKED);
						this->SendEvent(m_id,GUI_BUTTON_CHECKED,p);
					}
				}
				return true;
			case GUI_CONTROL_MOUSE_RELEASE:
				
				return true;
			case GUI_CONTROL_MOUSE_ENTER:
				{
					GUI_CONTROL_STATE state = this->GetState();
					if( state != GUI_CONTROL_STATE_CHECKED )
					{
						this->SetState(GUI_CONTROL_STATE_MOUSEOVER);
					}
				}
				return true;
			case GUI_CONTROL_MOUSE_LEAVE:
				{
					GUI_CONTROL_STATE state = this->GetState();
					if( state == GUI_CONTROL_STATE_MOUSEOVER )
					{
						this->SetState(GUI_CONTROL_STATE_NORMAL);
					}
				}
				return true;
			}
		}
		else if( m_style & BUTTON_SCROLLBAR )
		{
			
			switch( msg )
			{
			case GUI_CONTROL_MOUSE_PRESSED:
				{
					this->SetState(GUI_CONTROL_STATE_PRESSED);
				}
				break;
			case GUI_CONTROL_MOUSE_RELEASE:
				if( this->ContainPoint(pt) )
				{	
					this->SetState(GUI_CONTROL_STATE_MOUSEOVER);
				}
				else
					this->SetState(GUI_CONTROL_STATE_NORMAL);
				break;
			case GUI_CONTROL_MOUSE_ENTER:
				{
					this->SetState(GUI_CONTROL_STATE_MOUSEOVER);
				}
				break;
			case GUI_CONTROL_MOUSE_LEAVE:
				{
					this->SetState(GUI_CONTROL_STATE_NORMAL);
				}
				break;
			}
		}
		return false;
	}
	return false;
}

//----------------------------------------------------------------------------------------------------
void CGuiButtonRender::Render(Real timeDelta)
{
	const stRectI & oldrc = RenderSystem::GetSingleton()->SetClipRect(m_rControl.GetClientRect());
	for( vector<CGuiElementInstance*>::iterator iter = m_elementinstances.begin(); 
	 iter != m_elementinstances.end(); ++iter )
	{
		CGuiElementInstance* instance = (*iter);
		if( instance )
		{
			
			instance->Render(&m_rControl,
				m_rControl.GetClientRect(),
				timeDelta);
			instance->Render(&m_rControl,
				m_rControl.GetClientRect(),
				m_rControl.m_strText,
				m_rControl.m_font,
				m_rControl.m_dwColor,HMIDDLE_ALIGN | VMIDDLE_ALIGN,
				timeDelta);
		}
	}
	RenderSystem::GetSingleton()->SetClipRect(oldrc);

}

